<link href="/htqlmba/libraries/css/capnhatdv.css" type="text/css" rel="stylesheet">
﻿<?php
ob_start();
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/libraries/PHPExcel_1.7.9_doc/Classes/PHPExcel.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/libraries/PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/libraries/PHPExcel_1.7.9_doc/Classes/PHPExcel/Writer/Excel2007.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/libraries/PHPExcel_1.7.9_doc/Classes/PHPExcel.php');
/** PHPExcel_Writer_Excel2007 */
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/libraries/PHPExcel_1.7.9_doc/Classes/PHPExcel/Writer/Excel2007.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/configs/database.php');
$madv = $_GET['madv'];
$congsuat = $_GET['congsuat'];

if ($madv != 'tatca') {
    $db = new database();
    $db->setQuery("select * from don_vi where MA_DON_VI = '" . $madv . "'");
    $re = $db->fetchAll();

    $row = mysql_fetch_array($re);
    $tendv = $row['TEN_DON_VI'];

    //Lay danh sach may don vi tung so huu		
    $db->setQuery("select * from chi_tiet_so_huu where MA_DON_VI = '" . $madv . "' group by SO_N0");
    $dong = $db->fetchAll();
    $mang = array();
    $i = 0;

    while ($in = mysql_fetch_array($dong)) {
        $mang[$i] = $in['SO_N0'];

        $i++;
    }
    $mang2 = array();
    $k = 0;
    for ($j = 0; $j < $i; $j++) {
        $db->setQuery("select * from chi_tiet_so_huu where SO_N0 = '" . $mang[$j] . "' order by NGAY_CHUYEN_DEN desc");
        $truyvan = $db->fetchAll();
        $madvtimdc = mysql_fetch_array($truyvan);

        if ($madvtimdc['MA_DON_VI'] == $madv) {
            $mang2[$k] = $madvtimdc['SO_N0'];
            $k++;
        }
    }
    if (count($mang2) == 0) {
        echo "<p align='center'>Hiện tại đơn vị <b>" . $tendv . "</b> không sở hữu máy biến áp nào để thống kê </p>";
    } else {
        if ($congsuat != 'tatca') {
            $dem = 0;

            $i = 0;

            for ($i = 0; $i < count($mang2); $i++) {
                $db->setQuery("select count(*) as soluong from may_bien_ap where SO_N0 = '" . $mang2[$i] . "' and CONG_SUAT = " . $congsuat);
                $truyvan = $db->fetchAll();
                while ($ketqua = mysql_fetch_array($truyvan)) {
                    if ($ketqua['soluong'] == 1)
                        $dem++;
                }
            }
            // Khac tat ca
            echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
            echo "<tr>";
            echo "<td colspan ='3' align ='center' height = '30' class='TableHeader1'>Bảng Thống Kê Theo Công Suất</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td width='200' align ='center' class='TableHeaderbc'>Tên Đơn Vị</td>";
            echo "<td width='200' align ='center' class='TableHeaderbc'>Công Suất (kVA)</td>";
            echo "<td width='100' align ='center' class='TableHeaderbc'>Số Lượng</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
            echo "<td width='200' align ='center' class='TableField2'>" . $congsuat . "</td>";
            echo "<td width='100' align ='center' class='TableField2'>" . $dem . "</td>";
            echo "</tr>";

            echo "<tr>";
            echo "<td colspan ='3' align='right'>  Tổng số: " . $dem . " máy</td> ";

            echo "</tr>";
            echo "</table>";
        }
        else {
            // Truong hop cong suat la tat ca
            $tongtungdv = 0;
            echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
            echo "<tr>";
            echo "<td colspan ='3' align ='center' height = '30' class='TableHeader1' >Bảng Thống Kê Theo Công Suất</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td width='200' align ='center' class='TableHeaderbc'>Tên Đơn Vị</td>";
            echo "<td width='200' align ='center' class='TableHeaderbc'>Công Suất (kVA)</td>";
            echo "<td width='100' align ='center' class='TableHeaderbc'>Số Lượng</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
            echo "<td colspan ='2' width='400' align ='center'>";
            echo "<table align ='center' width='100%' border = '0'  cellspacing='0' cellpadding='0'>";
            $db->setQuery("select * from may_bien_ap  group by CONG_SUAT");
            $re = $db->fetchAll();

            while ($row = mysql_fetch_array($re)) {

                $cs = $row['CONG_SUAT'];
                $dem = 0;
                $i = 0;
                for ($i = 0; $i < count($mang2); $i++) {
                    $db->setQuery("select count(*) as soluong from may_bien_ap where SO_N0 = '" . $mang2[$i] . "' and CONG_SUAT = " . $cs);
                    $truyvan = $db->fetchAll();

                    while ($ketqua = mysql_fetch_array($truyvan)) {
                        if ($ketqua['soluong'] == 1)
                            $dem++;
                    }//het vong if
                }// het vong for

                if ($dem != 0) {
                    echo "<tr>";
                    $tongtungdv = $tongtungdv + $dem;
                    echo "<td width='200' align ='center' class='TableField2'>" . $cs . "</td>";
                    echo "<td width='100' align ='center' class='TableField2'>" . $dem . "</td>";
                    echo "</tr>";
                }
            }//het vong while
            echo "</table>";
            echo "</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td colspan ='3' align='right'>  Tổng số: " . $tongtungdv . " máy </td> ";

            echo "</tr>";
            echo "</table>";
        }// ket thuc else cua cong suat
    } // vong if cua 1 don vi
} else {// else cua tat ca don vi
    $db1 = new database();
    $tongmaycacdv = 0;
    echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
    echo "<tr>";
    echo "<td colspan ='3' align ='center' height = '30' class='TableHeader1'>Bảng Thống Kê Theo Công Suất</td>";

    echo "</tr>";
    echo "<tr>";
    echo "<td width='200' align ='center' class='TableHeaderbc'>Tên Đơn Vị</td>";
    echo "<td width='200' align ='center' class='TableHeaderbc'>Công Suất (kVA)</td>";
    echo "<td width='100' align ='center' class='TableHeaderbc'>Số Lượng</td>";
    echo "</tr>";
    $dsdv = "";
    $db1->setQuery("select * from don_vi as dv, tai_khoan as tk where dv.MA_DON_VI = tk.MA_DON_VI and  tk.QUYEN = 3");
    $re = $db1->fetchAll();
    while ($row = mysql_fetch_array($re)) {
        $tendv = $row['TEN_DON_VI'];
        $madv = $row['MA_DON_VI'];
        //Lay danh sach may don vi tung so huu		
        $db1->setQuery("select * from chi_tiet_so_huu where MA_DON_VI = '" . $madv . "' group by SO_N0");
        $dong = $db1->fetchAll();
        $mang = array();
        $i = 0;

        while ($in = mysql_fetch_array($dong)) {
            $mang[$i] = $in['SO_N0'];
            $i++;
        }
        $mang2 = array();
        $k = 0;

        for ($j = 0; $j < $i; $j++) {//for 
            $db1->setQuery("select * from chi_tiet_so_huu where SO_N0 = '" . $mang[$j] . "' 				order by 	NGAY_CHUYEN_DEN desc");
            $truyvan = $db1->fetchAll();
            $madvtimdc = mysql_fetch_array($truyvan);

            if ($madvtimdc['MA_DON_VI'] == $madv) {
                $mang2[$k] = $madvtimdc['SO_N0'];


                $k++;
            }
        }//for
        if (count($mang2) == 0)
            $dsdv = $dsdv . ", " . $tendv;

        else {//else count
            if ($congsuat != 'tatca') {

                $dem = 0;
                $i = 0;
                for ($i = 0; $i < count($mang2); $i++) {
                    $db1->setQuery("select count(*) as soluong from may_bien_ap where SO_N0 = '" . $mang2[$i] . "' and CONG_SUAT = " . $congsuat);
                    $truyvan = $db1->fetchAll();

                    while ($ketqua = mysql_fetch_array($truyvan)) {
                        if ($ketqua['soluong'] == 1)
                            $dem++;
                    }
                }

                $tongmaycacdv = $tongmaycacdv + $dem;
                echo "<tr>";
                echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
                echo "<td width='200' align ='center' class='TableField2'>" . $congsuat . "</td>";
                echo "<td width='100' align ='center' class='TableField2'>" . $dem . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<td colspan ='3' align='right'>  Tổng số: " . $dem . " máy </td> ";

                echo "</tr>";
            }


            else {
                // Truong hop cong suat la tat ca
                $tongtungdv = 0;

                echo "<tr>";
                echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
                echo "<td colspan ='2' width='400' align ='center' >";
                echo "<table align ='center' border = '0' cellspacing='0' cellpadding='0' >";
                $db1->setQuery("select * from may_bien_ap  group by CONG_SUAT");
                $s = $db1->fetchAll();

                while ($r = mysql_fetch_array($s)) {

                    $cs = $r['CONG_SUAT'];
                    $dem = 0;
                    $i = 0;
                    for ($i = 0; $i < count($mang2); $i++) {
                        $db1->setQuery("select count(*) as soluong from may_bien_ap where SO_N0 = '" . $mang2[$i] . "' and CONG_SUAT = " . $cs);
                        $t = $db1->fetchAll();

                        while ($q = mysql_fetch_array($t)) {
                            if ($q['soluong'] == 1)
                                $dem++;
                        }//het vong if
                    }// het vong for

                    if ($dem != 0) {
                        $tongmaycacdv = $tongmaycacdv + $dem;
                        echo "<tr>";
                        $tongtungdv = $tongtungdv + $dem;
                        echo "<td width='200' align ='center' class='TableField2'>" . $cs . "</td>";
                        echo "<td width='200' align ='center' class='TableField2'>" . $dem . "</td>";
                        echo "</tr>";
                    }
                }//het vong while
                echo "</table>";
                echo "</td>";
                echo "</tr>";
                echo "<tr>";
                echo "<td colspan ='3' align='right'>  Tổng số: " . $tongtungdv . " máy </td> ";

                echo "</tr>";
            }// ket thuc else cua cong suat
        }//else count
    }
    echo "<tr>";
    echo "<td colspan ='3'>  Tổng số tất cả các máy theo điều kiện trên: " . $tongmaycacdv . " máy </td> ";
    echo "</tr>";
    echo "</table> <br>";
    if ($dsdv != "") {
        $dsdv = ltrim($dsdv, ",");
        echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
        echo "<tr>";
        echo "<td width='200' align ='center' class='TableField2'> Danh sách các đơn vị không sở hữu MBA: " . "<br>" . $dsdv . "</td>";
        echo "</tr>";
        echo "</table>";
    }
}
echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
echo "<tr>";
echo "<br>";
echo "<td width='600' align ='right'><input class='btn_x' type='submit' name='inexcelcs' id='button' value='Xuất ra File Excel'></td>";
echo "</tr>";
echo "</table>";
?>